home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / Intset.sml < prev    next >
Encoding:
Text File  |  1996-07-03  |  9.6 KB  |  333 lines  |  [TEXT/R*ch]

  1. (* Intset -- modified for Moscow ML from SML/NJ library v. 0.2.
  2.  *
  3.  * COPYRIGHT (c) 1993 by AT&T Bell Laboratories.  
  4.  * See file mosml/copyrght/copyrght.att for details.
  5.  *
  6.  * This code was adapted from Stephen Adams' binary tree implementation
  7.  * of applicative integer sets.
  8.  *
  9.  *  Copyright 1992 Stephen Adams.
  10.  *
  11.  *  This software may be used freely provided that:
  12.  *    1. This copyright notice is attached to any copy, derived work,
  13.  *       or work including all or part of this software.
  14.  *    2. Any derived work must contain a prominent notice stating that
  15.  *       it has been altered from the original.
  16.  *
  17.  *  Altered to conform to SML library interface - Emden Gansner
  18.  *
  19.  *
  20.  * Name(s): Stephen Adams.
  21.  * Department, Institution: Electronics & Computer Science,
  22.  *    University of Southampton
  23.  * Address:  Electronics & Computer Science
  24.  *           University of Southampton
  25.  *           Southampton  SO9 5NH
  26.  *           Great Britian
  27.  * E-mail:   sra@ecs.soton.ac.uk
  28.  *
  29.  * Comments:
  30.  *
  31.  *   1.  The implementation is based on Binary search trees of Bounded
  32.  *       Balance, similar to Nievergelt & Reingold, SIAM J. Computing
  33.  *       2(1), March 1973.  The main advantage of these trees is that
  34.  *       they keep the size of the tree in the node, giving a constant
  35.  *       time size operation.
  36.  *
  37.  *   2.  The bounded balance criterion is simpler than N&R's alpha.
  38.  *       Simply, one subtree must not have more than `weight' times as
  39.  *       many elements as the opposite subtree.  Rebalancing is
  40.  *       guaranteed to reinstate the criterion for weight>2.23, but
  41.  *       the occasional incorrect behaviour for weight=2 is not
  42.  *       detrimental to performance.
  43.  *
  44.  *   3.  There are two implementations of union.  The default,
  45.  *       hedge_union, is much more complex and usually 20% faster.  I
  46.  *       am not sure that the performance increase warrants the
  47.  *       complexity (and time it took to write), but I am leaving it
  48.  *       in for the competition.  It is derived from the original
  49.  *       union by replacing the split_lt(gt) operations with a lazy
  50.  *       version. The `obvious' version is called old_union.
  51.  *
  52.  *   4.  Most time is spent in T', the rebalancing constructor.  If my
  53.  *       understanding of the output of *<file> in the sml batch
  54.  *       compiler is correct then the code produced by NJSML 0.75
  55.  *       (sparc) for the final case is very disappointing.  Most
  56.  *       invocations fall through to this case and most of these cases
  57.  *       fall to the else part, i.e. the plain contructor,
  58.  *       T(v,ln+rn+1,l,r).  The poor code allocates a 16 word vector
  59.  *       and saves lots of registers into it.  In the common case it
  60.  *       then retrieves a few of the registers and allocates the 5
  61.  *       word T node.  The values that it retrieves were live in
  62.  *       registers before the massive save.
  63.  *)
  64.  
  65. fun wt (i : int) = 3 * i
  66.  
  67. datatype Set = E | T of int * int * Set * Set
  68.  
  69. fun size E = 0
  70.   | size (T(_,n,_,_)) = n
  71.  
  72. (*fun N(v,l,r) = T(v,1+size(l)+size(r),l,r)*)
  73. fun N(v,E,              E)               = T(v,1,E,E)
  74.   | N(v,E,              r as T(_,n,_,_)) = T(v,n+1,E,r)
  75.   | N(v,l as T(_,n,_,_),E)               = T(v,n+1,l,E)
  76.   | N(v,l as T(_,n,_,_),r as T(_,m,_,_)) = T(v,n+m+1,l,r)
  77.  
  78. fun single_L (a,x,T(b,_,y,z)) = N(b,N(a,x,y),z)
  79.   | single_L _ = raise Match
  80. fun single_R (b,T(a,_,x,y),z) = N(a,x,N(b,y,z))
  81.   | single_R _ = raise Match
  82. fun double_L (a,w,T(c,_,T(b,_,x,y),z)) = N(b,N(a,w,x),N(c,y,z))
  83.   | double_L _ = raise Match
  84. fun double_R (c,T(a,_,w,T(b,_,x,y)),z) = N(b,N(a,w,x),N(c,y,z))
  85.   | double_R _ = raise Match
  86.  
  87. fun T' (v,E,E) = T(v,1,E,E)
  88.   | T' (v,E,r as T(_,_,E,E))     = T(v,2,E,r)
  89.   | T' (v,l as T(_,_,E,E),E)     = T(v,2,l,E)
  90.  
  91.   | T' (p as (_,E,T(_,_,T(_,_,_,_),E))) = double_L p
  92.   | T' (p as (_,T(_,_,E,T(_,_,_,_)),E)) = double_R p
  93.  
  94.   (* these cases almost never happen with small weight*)
  95.   | T' (p as (_,E,T(_,_,T(_,ln,_,_),T(_,rn,_,_)))) =
  96.     if ln<rn then single_L p else double_L p
  97.   | T' (p as (_,T(_,_,T(_,ln,_,_),T(_,rn,_,_)),E)) =
  98.     if ln>rn then single_R p else double_R p
  99.  
  100.   | T' (p as (_,E,T(_,_,E,_)))  = single_L p
  101.   | T' (p as (_,T(_,_,_,E),E))  = single_R p
  102.  
  103.   | T' (p as (v,l as T(lv,ln,ll,lr),r as T(rv,rn,rl,rr))) =
  104.     if rn>=wt ln then (*right is too big*)
  105.         let val rln = size rl
  106.         val rrn = size rr
  107.         in
  108.         if rln < rrn then  single_L p  else  double_L p
  109.         end
  110.         
  111.     else if ln>=wt rn then  (*left is too big*)
  112.         let val lln = size ll
  113.         val lrn = size lr
  114.         in
  115.         if lrn < lln then  single_R p  else  double_R p
  116.         end
  117.  
  118.     else
  119.          T(v,ln+rn+1,l,r)
  120.  
  121. fun addt t x =
  122.     let fun h E = T(x,1,E,E)
  123.       | h (set as T(v,_,l,r)) =
  124.         if x<v then T'(v, h l, r)
  125.         else if x>v then T'(v, l, h r)
  126.           else set
  127.     in h t end
  128.  
  129. fun concat3 E v r = addt r v
  130.   | concat3 l v E = addt l v
  131.   | concat3 (l as T(v1,n1,l1,r1)) v (r as T(v2,n2,l2,r2)) =
  132.     if wt n1 < n2 then T'(v2, concat3 l v l2,r2)
  133.     else if wt n2 < n1 then T'(v1,l1,concat3 r1 v r)
  134.     else N(v,l,r)
  135.  
  136. fun split_lt E x = E
  137.   | split_lt (t as T(v,_,l,r)) x =
  138.     if v>x then split_lt l x
  139.     else if v<x then concat3 l v (split_lt r x)
  140.     else l
  141.  
  142. fun split_gt E x = E
  143.   | split_gt (t as T(v,_,l,r)) x =
  144.     if v<x then split_gt r x
  145.     else if v>x then concat3 (split_gt l x) v r
  146.     else r
  147.  
  148. fun min (T(v,_,E,_)) = v
  149.   | min (T(v,_,l,_)) = min l
  150.   | min _            = raise Match
  151. and delete' (E,r) = r
  152.   | delete' (l,E) = l
  153.   | delete' (l,r) = 
  154.     let val min_elt = min r 
  155.     in T'(min_elt,l,delmin r) end
  156. and delmin (T(_,_,E,r)) = r
  157.   | delmin (T(v,_,l,r)) = T'(v,delmin l,r)
  158.   | delmin _ = raise Match
  159.  
  160. fun concat E  s2 = s2
  161.   | concat s1 E  = s1
  162.   | concat (t1 as T(v1,n1,l1,r1)) (t2 as T(v2,n2,l2,r2)) =
  163.     if wt n1 < n2 then T'(v2, concat t1 l2, r2)
  164.     else if wt n2 < n1 then T'(v1,l1, concat r1 t2)
  165.          else T'(min t2,t1, delmin t2)
  166.  
  167. type  intset = Set
  168.  
  169. exception NotFound
  170.  
  171. val empty = E
  172.     
  173. fun singleton x = T(x,1,E,E)
  174.  
  175. local
  176.     fun trim lo hi E = E
  177.       | trim lo hi (s as T(v,_,l,r)) =
  178.     if  v<=lo  then  trim lo hi r
  179.     else if  v>=hi  then  trim lo hi l
  180.     else  s
  181.             
  182.     fun uni_bd s E lo hi = s
  183.       | uni_bd E (T(v,_,l,r)) lo hi = 
  184.     concat3 (split_gt l lo) v (split_lt r hi)
  185.       | uni_bd (T(v,_,l1,r1)) (s2 as T(v2,_,l2,r2)) lo hi =
  186.     concat3 (uni_bd l1 (trim lo v s2) lo v)
  187.         v
  188.         (uni_bd r1 (trim v hi s2) v hi)
  189.     (* inv:  lo < v < hi *)
  190.  
  191.    (*all the other versions of uni and trim are
  192.    specializations of the above two functions with
  193.    lo=-infinity and/or hi=+infinity *)
  194.  
  195.     fun trim_lo _ E = E
  196.       | trim_lo lo (s as T(v,_,_,r)) =
  197.     if v<=lo then trim_lo lo r else s
  198.     fun trim_hi _ E = E
  199.       | trim_hi hi (s as T(v,_,l,_)) =
  200.     if v>=hi then trim_hi hi l else s
  201.             
  202.     fun uni_hi s E hi = s
  203.       | uni_hi E (T(v,_,l,r)) hi = 
  204.     concat3 l v (split_lt r hi)
  205.       | uni_hi (T(v,_,l1,r1)) (s2 as T(v2,_,l2,r2)) hi =
  206.     concat3 (uni_hi l1 (trim_hi v s2) v)
  207.         v 
  208.         (uni_bd r1 (trim v hi s2) v hi)
  209.     
  210.     fun uni_lo s E lo = s
  211.       | uni_lo E (T(v,_,l,r)) lo = 
  212.     concat3 (split_gt l lo) v r
  213.       | uni_lo (T(v,_,l1,r1)) (s2 as T(v2,_,l2,r2)) lo =
  214.     concat3 (uni_bd l1 (trim lo v s2) lo v)
  215.         v 
  216.         (uni_lo r1 (trim_lo v s2) v)
  217.     
  218.     fun uni (s,E) = s
  219.       | uni (E,s as T(v,_,l,r)) = s
  220.       | uni (T(v,_,l1,r1), s2 as T(v2,_,l2,r2)) =
  221.     concat3 (uni_hi l1 (trim_hi v s2) v)
  222.         v
  223.         (uni_lo r1 (trim_lo v s2) v)
  224. in
  225.     val union = uni
  226. end
  227.  
  228. fun addList (s,l) = List.foldl (fn (i,s) => addt s i) s l
  229.  
  230. fun add(s, i) = addt s i
  231.  
  232. fun difference (E,s)  = E
  233.   | difference (s,E)  = s
  234.   | difference (s, T(v,_,l,r)) =
  235.     let val l2 = split_lt s v
  236.     val r2 = split_gt s v
  237.     in
  238.     concat (difference(l2,l)) (difference(r2,r))
  239.     end
  240.  
  241. fun membert set x =
  242.     let fun mem E = false
  243.       | mem (T(v,_,l,r)) =
  244.         if x<v then mem l else if x>v then mem r else true
  245.     in mem set end
  246.  
  247. fun member (set,x) = membert set x
  248.  
  249. (*fun intersection (a,b) = difference(a,difference(a,b))*)
  250.  
  251. fun intersection (E,_) = E
  252.   | intersection (_,E) = E
  253.   | intersection (s, T(v,_,l,r)) =
  254.     let val l2 = split_lt s v
  255.     val r2 = split_gt s v
  256.     in
  257.     if membert s v then
  258.         concat3 (intersection(l2,l)) v (intersection(r2,r))
  259.     else
  260.         concat (intersection(l2,l)) (intersection(r2,r))
  261.     end
  262.  
  263. fun numItems E = 0
  264.   | numItems (T(_,n,_,_)) = n
  265.  
  266. fun isEmpty E = true
  267.   | isEmpty _ = false
  268.  
  269. fun delete (E,x) = raise NotFound
  270.   | delete (set as T(v,_,l,r),x) =
  271.     if x<v then T'(v,delete(l,x),r)
  272.     else if x>v then T'(v,l,delete(r,x))
  273.     else delete'(l,r)
  274.  
  275. fun foldr f base set = 
  276.     let    fun fold' base E = base
  277.       | fold' base (T(v,_,l,r)) = fold' (f(v, fold' base r)) l
  278.     in fold' base set end
  279.  
  280. fun foldl f base set = 
  281.     let    fun fold' base E = base
  282.       | fold' base (T(v,_,l,r)) = fold' (f(v, fold' base l)) r
  283.     in fold' base set end
  284.  
  285. fun app f set = 
  286.     let    fun app' E = ()
  287.       | app'(T(v,_,l,r)) = (app' l; f v; app' r)
  288.     in app' set end
  289.  
  290. fun revapp f set = 
  291.     let    fun app' E = ()
  292.       | app'(T(v,_,l,r)) = (app' r; f v; app' l)
  293.     in app' set end
  294.  
  295. local
  296.     (* true if every item in t is in t' *)
  297.     fun treeIn t t' =
  298.     let
  299.         fun isIn E = true
  300.           | isIn (T(v,_,E,E)) = membert t' v
  301.           | isIn (T(v,_,l,E)) = 
  302.         membert t' v andalso isIn l
  303.           | isIn (T(v,_,E,r)) = 
  304.         membert t' v andalso isIn r
  305.           | isIn (T(v,_,l,r)) = 
  306.         membert t' v andalso isIn l andalso isIn r
  307.         in
  308.         isIn t
  309.         end
  310. in
  311.     fun isSubset (E,_) = true
  312.       | isSubset (_,E) = false
  313.       | isSubset (t as T(_,n,_,_),t' as T(_,n',_,_)) =
  314.     (n<=n') andalso treeIn t t'
  315.  
  316.     fun equal (E,E) = true
  317.       | equal (t as T(_,n,_,_),t' as T(_,n',_,_)) =
  318.     (n=n') andalso treeIn t t'
  319.       | equal _ = false
  320. end
  321.  
  322. fun find p set = 
  323.     let fun h E            = NONE
  324.       | h (T(v,_,l,r)) =
  325.         if p v then SOME v
  326.         else case h l of
  327.         NONE => h r
  328.           | a => a 
  329.     in h set end;
  330.  
  331. fun listItems set = foldr (op::) [] set
  332.  
  333.